import { _decorator, color, Color, Component, EditBox, EventTouch, game, Label, Node, Size, Vec2, Vec3 } from 'cc';
import { gameState } from './gameState';
import { eventDispatcher } from './eventDispatcher';
import { layerAction_001 } from './layerAction-001';
import { editClick } from './editClick';
const { ccclass, property } = _decorator;

@ccclass('editCtlAction')
export class editCtlAction extends Component {
    @property(Label) label_model_value: Label = null;
    @property(Label) label_block_size: Label = null;
    @property(Label) label_msg: Label = null;
    @property(Label) label_position: Label = null;

    @property(EditBox) position_x: EditBox = null;
    @property(EditBox) position_y: EditBox = null;
    @property(EditBox) position_data: EditBox = null;

    @property(layerAction_001) layer1:layerAction_001 = null;

    refresh_model() {
        switch (gameState.edit_model) {
            case 1:
                this.label_model_value.string = "add-block";
                break;
            case 2:
                this.label_model_value.string = "del-block";
                break;
            case 3:
                this.label_model_value.string = "add-grid";
                break;
            case 4:
                this.label_model_value.string = "del-grid";
                break;
        }

    }
    click_model(e: EventTouch, args: string) {
        gameState.edit_model = Number(args);
        this.refresh_model();
    }
    click_import(){
        let data = this.position_data.string;
        if(data.trim().length<5){
            this.label_msg.string = "data is null";
            return;
        }
        this.click_clear_all();
        let arr = JSON.parse(data);
        arr.forEach(ele =>{
            this.layer1.add_block_by_local_pos(new Vec3(ele.x,ele.y));
        })
        eventDispatcher.get_target().emit(eventDispatcher.TIPS_MSG,"import success");
        this.layer1.refresh_shadow();
        this.update_block_size();
    }
    click_export(){
        let children = this.layer1.get_children();
        if(children.length%3!==0 || children.length===0){
            this.label_msg.string = "block size must be a multiple 3";
            return;
        }
        let arr:any[] = [];
        children.forEach(element => {
            let pos = element.getPosition();
            arr.push({x:Math.ceil(pos.x),y:Math.ceil(pos.y)})
        });
        let ret = JSON.stringify(arr);
        this.position_data.string = ret;
        eventDispatcher.get_target().emit(eventDispatcher.TIPS_MSG,"export complete");
    }
    click_clear_all(){
        this.layer1.clear_all();
        this.update_block_size();
        eventDispatcher.get_target().emit(eventDispatcher.TIPS_MSG,"clear complete");
    }
    click_custom(){
        let x = this.position_x.string;
        let y = this.position_y.string;
        this.node.parent.getChildByName("edit").getComponent(editClick).add_grid(Number(x),Number(y),new Size(10,10))
    }
    set_pos(p:Vec3){
       this.label_position.string = ""+Math.floor(p.x)+","+Math.floor(p.y);
    }
    update_block_size(){
        let size = this.layer1.get_children().length;
        this.label_block_size.string = ""+size;
        let color = null;
        if(size%3===0){
            color = new Color(Color.WHITE);
        }else{
            color = new Color(Color.RED);
        }
        this.label_block_size.color = color;
    }

    start() {
        this.refresh_model();
        eventDispatcher.get_target().on(eventDispatcher.UPDATE_BLOCK_SIZE,this.update_block_size,this);
        this.update_block_size();
    }

    update(deltaTime: number) {
    }
}


